package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.ICourseDao;
import com.zhijiaoyun.dao.ICourseHistoryDao;
import com.zhijiaoyun.model.CourseHistory;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ICourseHistoryService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "courseHistoryService")
public class CourseHistoryServiceImpl implements ICourseHistoryService {

	@Resource
	ICourseHistoryDao courseHistoryDao;
	
	@Resource
	ICourseDao courseDao;
	
	@Override
	public CourseHistory getCourseHistoryByCourseIdUserId(Long courseId, Long userId) {
		return courseHistoryDao.selectByCourseIdUserId(courseId, userId);
	}

	@Override
	public void addCourseHistory(CourseHistory courseHistory) {
		courseHistoryDao.insert(courseHistory);
	}

	@Override
	public PageList<CourseHistory> pageCourseHistoryByParams(Integer pageIndex, Long userId) {
		int count = courseHistoryDao.countByParams(userId);
		PageData pageData = new PageData();
		List<CourseHistory> list = new ArrayList<>();
		PageList<CourseHistory> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
			list = courseHistoryDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), userId);
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public List<CourseHistory> getCourseHistoryByUserId(Integer pageIndex, Long userId) {
		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
		return courseHistoryDao.selectByUserId(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId);
	}

	@Override
	public void modifyCourseHistory(CourseHistory courseHistory) {
		courseHistoryDao.updateByPrimaryKey(courseHistory);
	}

}
